﻿Public Class WPFHost
	Public WithEvents Parent As Control
	Public Rectangle As Rectangle
	Dim G As Graphics

	Sub New(Parent As Control)
		Me.Parent = Parent
		G = Parent.CreateGraphics
	End Sub

	ReadOnly Property Font As Font
		Get
			Return Parent.Font
		End Get
	End Property

	Private Sub Parent_FontChanged(sender As Object, e As System.EventArgs) Handles Parent.FontChanged
		ResizeContent()
	End Sub

	Private Sub Parent_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Parent.MouseDown
		If Content Is Nothing Then Return
		Content.CheckMouseEvent(e, "down")
	End Sub

	Private Sub Parent_MouseLeave(sender As Object, e As System.EventArgs) Handles Parent.MouseLeave
		If Content Is Nothing Then Return
		Content.CheckMouseEvent(New MouseEventArgs(MouseButtons.None, 0, -10000, -10000, 0), "enter")
	End Sub

	Private Sub Parent_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Parent.MouseMove
		If Content Is Nothing Then Return
		Content.CheckMouseEvent(e, "leave")
		Content.CheckMouseEvent(e, "enter")
	End Sub

	Private Sub Parent_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles Parent.Paint
		If Content Is Nothing Then Return
		Content.Draw(e)
	End Sub

	Private Sub Parent_SizeChanged(sender As Object, e As System.EventArgs) Handles Parent.SizeChanged
		Rectangle = Parent.ClientRectangle
		ResizeContent()
		Parent.Invalidate()
	End Sub

	Dim _Content As UIElement
	Property Content As UIElement
		Get
			Return _Content
		End Get
		Set(value As UIElement)
			_Content = value
			Content.TopHost = Me
			Content.Resize(Rectangle)
		End Set
	End Property

	Function MeasureText(Text As String, Font As Drawing.Font) As SizeF
		Return G.MeasureString(Text, Font)
	End Function

	Private Sub ResizeContent()
		If Not Content Is Nothing Then Content.Measure()
		If Not Content Is Nothing Then Content.Resize(Rectangle)
	End Sub

	Sub Invalidate(Rectangle As Drawing.Rectangle)
		Parent.Invalidate(Rectangle)
	End Sub

	Sub Remeasure()
		ResizeContent()
	End Sub

End Class
